from flask import request
from flask_restful import Resource, reqparse
from exts import api, db
from flask_docs import ApiDoc
from services.user_service import *
from utils.auth import auth_service
from flask import jsonify
from models.user import AdminUser, RoleType


class AdminUser(Resource):
    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def get(self, user_id=None):
        try:
            if user_id:
                return {
                    "message": "获取用户信息成功",
                    "user_list": user_service().get_user_info(user_id=user_id),
                    "code": 200,
                }  # 添加 code 信息
            else:
                return {
                    "message": "获取用户列表成功",
                    "user_list": user_service().get_all_users(),
                    "code": 200,
                }  # 添加 code 信息
        except Exception as e:
            return {"message": str(e), "code": 400}, 400

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def post(self):
        try:
            post_json = request.get_json()
            if post_json:
                user_info = user_service().create_user(post_json)
                return {
                    "message": f"用户{user_info['username']}创建成功",
                    "user_info": user_info,
                    "code": 200,  # 添加 code 信息
                }

            else:
                return {"message": "参数错误", "code": 400}, 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def put(self):
        try:
            put_json = request.get_json()
            print("put_json",put_json)
            if not put_json:
                return {"message": "参数不能为空", "code": 400}, 400
            if put_json:
                user_info = user_service().update_user(put_json)
                return {
                    "message": f"用户{user_info['username']}更新成功",
                    "user_info": user_info,
                    "code": 200,  # 添加 code 信息
                }
            else:
                return {"message": "参数错误", "code": 400}, 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def delete(self):
        try:
            delete_json = request.get_json()
            if not delete_json :
                return {"message": "参数不能为空", "code": 400}, 400
            if delete_json:
                user_info = user_service().delete_user(delete_json)
                return {
                    "message": f"用户{user_info['username']}删除成功",
                    "user_info": user_info,
                    "code": 200,  # 添加 code 信息
                }
            else:
                return {"message": "参数错误", "code": 400}, 400
        except Exception as e:
            return {"message": str(e), "code": 400}, 400
        finally:
            db.session.close()

    def options(self):
        return {"message": "Options request received", "code": 200}, 200


api.add_resource(AdminUser, "/admin/user", "/admin/user/<int:user_id>")
